<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="计划类型" prop="messageTypeId">
        <el-select v-model="queryParams.messageTypeId" placeholder="请选择计划类型" clearable size="small">
          <el-option label="1" value="" />
          <el-option label="2" value="" />
          <el-option label="3" value="" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:plan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:plan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:plan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:plan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="planId" />
      <el-table-column label="计划类型" align="center" prop="messageTypeId" />
      <el-table-column label="描述" show-overflow-tooltip="true" align="center" prop="descri" />
      <el-table-column label="名称" show-overflow-tooltip="true"  align="center" prop="name" />
      <el-table-column label="动作" show-overflow-tooltip="true" align="center" prop="content" />
      <el-table-column label="图片" align="center"  >
        <template slot-scope="scope" >
          <img :src="baseurl+ scope.row.pic" class="user-avatar">
        </template>
          <!--<img :src="baseurl+" class="user-avatar">-->
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:plan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:plan:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="计划类型" prop="messageTypeId">
          <el-input v-model="form.messageTypeId" placeholder="请输入计划类型" />
        </el-form-item>
        <el-form-item label="描述" prop="descri">
          <el-input v-model="form.descri" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="form.content" />
        </el-form-item>

        <el-form-item label="图片上传" prop="pic">
          <el-upload
            ref="upload"
            action="233"
                     :auto-upload="false"
                     :http-request="postPhoto"
                     list-type="picture-card"
                     :on-preview="handlePictureCardPreview"
                     :on-remove="handleRemove"
                     :limit="1"
                     :on-change="fail">
            <i class="el-icon-plus"></i>
          </el-upload>
          <span>只支持jpg与png格式文件上传</span>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt>
          </el-dialog>
        </el-form-item>


        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPlan, getPlan, delPlan, addPlan, updatePlan, exportPlan ,uploadPic} from "@/api/system/plan";
import Editor from '@/components/Editor';


export default {
  name: "Plan",
  components: {
    Editor,
  },

  data() {
    return {
      dialogVisible: false,

      baseurl : process.env.VUE_APP_BASE_API,

      dialogImageUrl:null,

      imgPost:null,

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        messageTypeId: null,
        descri: null,
        name: null,
        content: null,
        pic: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        messageTypeId: [
          { required: true, message: "计划类型不能为空", trigger: "blur" }
        ],
        descri: [
          { required: true, message: "描述不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "动作不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    fail (file) {
      this.dialogImageUrl = file.url;
      this.dialogImageUrl = new window.File([this.dialogImageUrl], file.name, {
        type: file.type
      });
     // 上面负责回显，调用的本地
      // this.dialogVisible = true;
      //这里我在data中初始化了一个变量，imgPost，给他赋值。
      this.imgPost = file.raw

    },
    handleRemove(file, fileList) {

      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },


    /** 查询列表 */
    getList() {
      this.loading = true;
      listPlan(this.queryParams).then(response => {
        this.planList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planId: null,
        messageTypeId: null,
        descri: null,
        name: null,
        content: null,
        pic: null,
        createTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.planId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const planId = row.planId || this.ids
      getPlan(planId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改计划";
      });
    },

    /** 提交按钮 */
    submitForm(file) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.planId != null) {
            let formData1 = new FormData();
            formData1.append("file", this.imgPost)
            uploadPic(formData1).then(res => {
              this.form.pic = res.imgUrl;
              console.log("i是否hi符合"+res.imgUrl);

              updatePlan(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
                this.$refs.upload.clearFiles();
              });
            });
          } else {
            let formData1 = new FormData();
            formData1.append("file", this.imgPost)
            uploadPic(formData1).then(res => {
              this.form.pic = res.imgUrl;
              console.log("i是否hi符合"+res.imgUrl);
              addPlan(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
                this.$refs.upload.clearFiles();
              });
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planIds = row.planId || this.ids;
      this.$confirm('是否确认删除编号为"' + planIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPlan(planIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPlan(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
<style>
  .user-avatar {
    cursor: pointer;
    width: 40px;
    height: 40px;
    border-radius: 10px;
  }



</style>
